package com.chocomania;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

import com.dao.ChocomaniaSqlLiteHelper;

public class ListaDeRecetas extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
		setContentView(R.layout.recetas);
		
		String whereClause = null;
		
		//TOMO EL ID DE LA CATEGORIA
        try{
			String idCategoria = getIntent().getExtras().getString("idCategoria");
	        if(idCategoria!=null){
	        	whereClause = "_id_categoria="+idCategoria;
	        }
        }catch(Exception e){}
        
		//this.deleteDatabase("chocomania.db");
		ChocomaniaSqlLiteHelper chocoDb = new ChocomaniaSqlLiteHelper(this);
		SQLiteDatabase db = chocoDb.getWritableDatabase();
		
        String[] columns = {"_id","nombre","tiempo_preparacion"};
        
        Cursor c = db.query("receta", columns, 
        		whereClause, null, null, null, null, null);
        startManagingCursor(c);
        
        // set this adapter as your ListActivity's adapter
		ListView listView = (ListView) findViewById(R.id.listaRecetas); 
		MyAdapter mListAdapter = new MyAdapter(ListaDeRecetas.this,c);
		listView.setAdapter(mListAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, 
					int posicion,long id) {
		    	    	
		    	Intent i = new Intent(ListaDeRecetas.this,DetalleDeReceta.class);
		    	i.putExtra("idReceta",((Long)id).toString()); //envio la receta
		    	i.putExtra("random","false"); //envio la receta
		    	startActivity(i);
			}
		});
		
		//BOTON VOLVER A LA PANTALLA ANTERIOR
        Button botonVolver = (Button) findViewById(R.id.btnVolverDeRecetas);
        String volverIniciando="false";
        try{
        	volverIniciando = getIntent().getExtras().getString("categorias");
        }catch(Exception e){}
        
        if(volverIniciando.equalsIgnoreCase("true")){
			botonVolver.setOnClickListener(new View.OnClickListener() {
			    public void onClick(View view) {
			    	finish();
			    }
			});
        }else{
			botonVolver.setOnClickListener(new View.OnClickListener() {
			    public void onClick(View view) {
			    	Intent i = new Intent(ListaDeRecetas.this,Inicio.class);
			    	startActivity(i);
			    }
			});
        }
    }
    
    private class MyAdapter extends ResourceCursorAdapter {

        public MyAdapter(Context context, Cursor cur) {
            super(context, R.layout.listview_fila, cur);
        }

        @Override
        public View newView(Context context, Cursor cur, ViewGroup parent) {
            LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return li.inflate(R.layout.listview_fila, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cur) {
        	
        	TextView tituloRecetaText = (TextView)view.findViewById(R.id.TextTituloReceta);
            TextView descripcionText = (TextView)view.findViewById(R.id.TextDescripcionReceta);
            
            tituloRecetaText.setText(cur.getString(cur.getColumnIndex("nombre")));
            descripcionText.setText("Tiempo de Preparacion: "+cur.getString(cur.getColumnIndex("tiempo_preparacion"))+" min.");
        }
    }

    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event){
		if(keyCode == KeyEvent.KEYCODE_BACK){ 
			
			String volverIniciando="false";
	        try{
	        	volverIniciando = getIntent().getExtras().getString("categorias");
	        }catch(Exception e){
	        	Intent i = new Intent(ListaDeRecetas.this,Inicio.class);
		    	startActivity(i);
	        }
	        
	        if(volverIniciando.equalsIgnoreCase("true")){
	        	Intent i = new Intent(ListaDeRecetas.this,Categorias.class);
		    	startActivity(i);
			}else{
		    	Intent i = new Intent(ListaDeRecetas.this,Inicio.class);
		    	startActivity(i);
	        }
		} 
		return false;
	}
}